// $(function () {
//     // findAll({});
//     page();
// });

function findAll(json) {
    //查询所有
    let data = myAjax("/back/user/findAll", json, "get");
//赋值，给table表格赋值
    setUserList(data.data);
}

function setUserList(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += ' <tr>\n' +
            '<td><input type="checkbox"  class="layui-input"></td>' +
            '                                    <td>' + data[i].id + '</td>\n' +
            '                                    <td>' + data[i].username + '</td>\n' +
            '                                    <td>' + data[i].sex + '</td>\n' +
            '                                    <td>' + data[i].phone + '</td>\n' +
            '                                    <td>' + data[i].address + '</td>\n' +
            '                                    <td class="td-status">\n' +
            '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (data[i].enable == 1 ? '' : 'layui-btn-disabled') + '">' + (data[i].enable == 1 ? '已启用' : '已停用') + '</span></td>\n' +
            '                                    <td class="td-manage">\n' +
            '                                      <a onclick="member_stop(this,' + data[i].id + ')" href="javascript:;"  title="' + (data[i].enable == 1 ? '停用' : '启用') + '">\n' +
            '                                        <i class="layui-icon">' + (data[i].enable == 1 ? '&#xe62f;' : '&#xe601;') + '</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="编辑"  onclick="xadmin.open(\'编辑\',\'member-edit.html\',600,400)" href="javascript:;">\n' +
            '                                        <i class="iconfont">&#xe64d;</i>\n' +
            '                                      </a>\n' +
            '                                      <a onclick="xadmin.open(\'修改密码\',\'member-password.html\',600,400)" title="修改密码" href="javascript:;">\n' +
            '                                        <i class="iconfont">&#xe645;</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="删除" onclick="member_del(this,\'要删除的id\')" href="javascript:;">\n' +
            '                                        <i class="iconfont">&#xe64c;</i>\n' +
            '                                      </a>\n' +
            '                                    </td>\n' +
            '                                  </tr>';
    }
    $("#tbody").html(html);
}

layui.use(['laydate', 'laypage', 'layer', 'form'], function () {
    var laypage = layui.laypage
        , layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;
    //监听提交
    form.on('submit(sreach)',
        function (data) {
            data = data.field;
            // console.log(data);
            //发异步，把数据提交给php
            page();
            return false;
        });
    page();
//执行一个laydate实例
    laydate.render({
        elem: '#start' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
        elem: '#end' //指定元素
    });


});

/*用户-停用*/
function member_stop(obj, id) {
    layer.confirm('确认要' + ($(obj).attr('title') == '启用' ? "启用" : "停用") + '吗？', function (index) {
        //发异步把用户状态进行更改
        let data = myAjax("/back/user/enable", {id: id}, "post");
        if (data.code == 0) {
            if ($(obj).attr('title') == '启用') {
                $(obj).attr('title', '停用')
                $(obj).find('i').html('&#xe62f;');
                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');

                layer.msg('已启用!', {icon: 5, time: 1000});
            } else {
                $(obj).attr('title', '启用')
                $(obj).find('i').html('&#xe601;');
                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!', {icon: 5, time: 1000});
            }
        }


    });
}

/*用户-删除*/
function member_del(obj, id) {
    layer.confirm('确认要删除吗？', function (index) {
        //发异步删除数据
        $(obj).parents("tr").remove();
        layer.msg('已删除!', {icon: 1, time: 1000});
    });
}

function getQueryData() {
    return {
        start: $("#start").val(),
        end: $("#end").val(),
        username: $("#username").val()
    }
}

function page() {
    console.log(getQueryData());
    layui.use(['laydate', 'laypage', 'layer', 'form'], function () {
        var laypage = layui.laypage
            , layer = layui.layer;
        //完整功能
        laypage.render({
            elem: 'demo7'
            , count: getCount(getQueryData())
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            , jump: function (obj) {
                // console.log(obj)
                let temp = getQueryData();
                let pageData = {
                    page: obj.curr,
                    limit: obj.limit
                };
                findAll(Object.assign(temp, pageData));
            }
        });
    });
}

function getCount(json) {
    let data = myAjax("/back/user/getCount", json, "get");
    return data.data;
}